package com.android.cgame02;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;

public class DrawImage {
	
	Matrix matrix = new Matrix();
	Bitmap img = null;
	public final int 
			TRANS_NONE = 0,
			TRANS_ROT90 = 5,
			TRANS_ROT180 = 3,
			TRANS_ROT270 = 6,
			TRANS_MIRROR = 2,
			TRANS_MIRROR_ROT90 = 7 ,
			TRANS_MIRROR_ROT180 = 1,
			TRANS_MIRROR_ROT270 = 4;
	public Bitmap mbitmap;
	public Canvas mcanvas;
	public int
			msrc_x = 0,
			msrc_y = 0,
			msrc_w = 0,
			msrc_h = 0,
			mdes_x = 0,
			mdes_y = 0,
			mtransform = 0;
	public DrawImage (Canvas canvas, Bitmap bitmap, int src_x,int src_y,int src_w,int src_h){
		this.mcanvas = canvas;
		this.mbitmap = bitmap;
		this.msrc_x = src_x;
		this.msrc_y = src_y;
		this.msrc_w = src_w;
		this.msrc_h = src_h;
	}

	public void transform(int transform) {
		matrix.reset();
		switch (transform) {
		case TRANS_NONE: {
			break;
		}
		case TRANS_ROT90: {
			matrix.preRotate(90);
			break;
		}
		case TRANS_ROT180: {
			matrix.preRotate(180);
			break;
		}
		case TRANS_ROT270: {
			matrix.preRotate(270);
			break;
		}
		case TRANS_MIRROR: {
			matrix.postScale(-1, 1);
			break;
		}
		case TRANS_MIRROR_ROT90: {
			matrix.postScale(1,-1);
			matrix.preRotate(90);
			break;
		}
		case TRANS_MIRROR_ROT180: {
			matrix.postScale(1,-1);
			break;
		}
		case TRANS_MIRROR_ROT270: {
			matrix.postScale(1,-1);
			matrix.preRotate(270);
			break;
		}
		default:
			throw new IllegalArgumentException("Bad transform");
		}
		img = Bitmap.createBitmap(mbitmap,msrc_x, msrc_y, msrc_w, msrc_h, matrix, true);
	}
	
	public void draw(int des_x, int des_y){
		if(img == null){
			matrix.reset();
			img = Bitmap.createBitmap(mbitmap,msrc_x, msrc_y, msrc_w, msrc_h, matrix, true);
		}
		mcanvas.drawBitmap(img, des_x, des_y, null);
	}
	
	public void clean(){
		img = null;
	}
	
	public int getWidth(){
		return img.getWidth();
	}
	
	public int getHight(){
		return img.getHeight();
	}
}
